//
//  TLST_saveListVC.m
//  Travel_World
//
//  Created by lanou on 15/10/17.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "TLST_saveListVC.h"
#import "SSCollectionDataTool.h"
#import "SSCollectionModel.h"
#import "TLST_discountCell.h"
#import "SSFirstPageWebView.h"
#import "MRCountryViewController.h"
#import "MRCityViewController.h"

@interface TLST_saveListVC ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) UISegmentedControl *saveSegVC;
@property (nonatomic, strong) UIScrollView *bigScrollor;

@property (nonatomic, strong) UITableView *disTableView;
@property (nonatomic, strong) NSMutableArray *disDataArr;

@property (nonatomic, strong) UITableView *countryTableView;
@property (nonatomic, strong) NSMutableArray *countryDataArr;

@property (nonatomic, strong) UITableView *cityTableView;
@property (nonatomic, strong) NSMutableArray *cityDataArr;

@property (nonatomic, strong) UIImageView *disPlece;
@property (nonatomic, strong) UIImageView *couPlace;
@property (nonatomic, strong) UIImageView *citPlace;
@end

@implementation TLST_saveListVC

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.158 green:0.404 blue:1.000 alpha:1.000];
    self.navigationController.navigationBar.layer.masksToBounds = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    [self createDiscountData];
    
    [self.disTableView reloadData];
    [self.countryTableView reloadData];
    [self.cityTableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.placeLabel removeFromSuperview];
    
    self.bigScrollor = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.bigScrollor.delegate = self;
    self.bigScrollor.pagingEnabled = YES;
    self.bigScrollor.showsHorizontalScrollIndicator = NO;
    self.bigScrollor.contentSize = CGSizeMake(WIDTH*3, HEIGHT);
    [self.view addSubview:self.bigScrollor];
    
    self.disPlece = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeImage-save.jpg"]];
    self.disPlece.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.couPlace = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeImage-save.jpg"]];
    self.couPlace.frame = CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
    self.citPlace = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeImage-save.jpg"]];
    self.citPlace.frame = CGRectMake(2*WIDTH, 0, WIDTH, HEIGHT);

    self.disTableView = [self createTableViewWithIndex:0 placeLabel:self.disPlece];
    self.countryTableView = [self createTableViewWithIndex:1 placeLabel:self.couPlace];
    self.cityTableView = [self createTableViewWithIndex:2 placeLabel:self.citPlace];
    
    [self createDiscountData];
    [self createSegVC];
}

- (UITableView *)createTableViewWithIndex:(NSInteger)index placeLabel:(UIImageView *)plaecLabel
{
    // 创建tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(index * WIDTH, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [[UIView alloc] init];
    tableView.rowHeight = 100;
    [self.bigScrollor addSubview:tableView];
    [self.bigScrollor addSubview:plaecLabel];
    return tableView;
}

- (void)createDiscountData
{
    self.disDataArr = [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:DiscountModel] selectAllCollectionDataModelType:DiscountModel];
    self.countryDataArr = [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:CountryModel] selectAllCollectionDataModelType:CountryModel];
    self.cityDataArr = [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:CityModel] selectAllCollectionDataModelType:CityModel];

    if (self.disDataArr.count > 0) {
        self.disPlece.hidden = YES;
    } else {
        self.disPlece.hidden = NO;
    }
    if (self.countryDataArr.count > 0) {
        self.couPlace.hidden = YES;
    } else {
        self.couPlace.hidden = NO;
    }
    if (self.cityDataArr.count > 0) {
        self.citPlace.hidden = YES;
    } else {
        self.citPlace.hidden = NO;
    }
}
#pragma mark tableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.disTableView) {
        return self.disDataArr.count;
        
    } else if (tableView == self.countryTableView) {
        return self.countryDataArr.count;
    } else {
        
        return self.cityDataArr.count;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TLST_discountCell *cell = [TLST_discountCell discountCellWithTableView:tableView];
    
    if (tableView == self.disTableView && self.disDataArr.count > indexPath.row) {
        cell.dataModel = self.disDataArr[indexPath.row];
        
    } else if (tableView == self.countryTableView && self.countryDataArr.count > indexPath.row) {
        cell.dataModel = self.countryDataArr[indexPath.row];
    } else {
        
        cell.dataModel = self.cityDataArr[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.disTableView && self.disDataArr.count > indexPath.row) {
        
        SSFirstPageWebView *firstWebVC = [[SSFirstPageWebView alloc] init];
        firstWebVC.title = @"折扣";
        
        SSCollectionModel *disModel = self.disDataArr[indexPath.row];
        firstWebVC.myURL = disModel.url;
        firstWebVC.collectionModel = disModel;
        
        [self.navigationController pushViewController:firstWebVC animated:YES];
        
    } else if (tableView  == self.countryTableView && self.countryDataArr.count > indexPath.row) {
        
        TLS_countryModel *countryModel = self.countryDataArr[indexPath.row];
    
        MRCountryViewController *countryVC = [[MRCountryViewController alloc] init];
        
        NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@&lat=38.88261036070509&lon=121.5397574228773&oauth_token=55d7f8a1931dedc7d5a8f24ab63426d0&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone5%%2C4&track_deviceid=41AE760B-510C-4778-AF4D-177B387E0F3B&track_os=ios%%25209.0.2&track_user_id=6852801&v=1",countryModel.Id];
        
        countryVC.url = url;

        [self.navigationController pushViewController:countryVC animated:YES];
        
    } else {
    
        TLS_cityModel *cityModel = self.cityDataArr[indexPath.row];
        MRCityViewController *cityVC = [[MRCityViewController alloc] init];
        
        NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88261036070509&lon=121.5397574228773&oauth_token=55d7f8a1931dedc7d5a8f24ab63426d0&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone5%%2C4&track_deviceid=41AE760B-510C-4778-AF4D-177B387E0F3B&track_os=ios%%25209.0.2&track_user_id=6852801&v=1",cityModel.Id];
        
        cityVC.url = url;
        
        [self.navigationController pushViewController:cityVC animated:YES];
    }

}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        if (tableView == self.disTableView) {
            
            [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:DiscountModel] deleteCollectoinData:self.disDataArr[indexPath.row] modelType:DiscountModel];
            [self.disDataArr removeObjectAtIndex:indexPath.row];
            [self.disTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
            
            if (self.disDataArr.count == 0) {
                self.disPlece.hidden = NO;
            }
            
        } else if (tableView == self.countryTableView) {
        
            [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:CountryModel] deleteCollectoinData:self.countryDataArr[indexPath.row] modelType:CountryModel];
            [self.countryDataArr removeObjectAtIndex:indexPath.row];
            [self.countryTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
            
            if (self.countryDataArr.count == 0) {
                self.couPlace.hidden = NO;
            }
        } else {
        
            [[SSCollectionDataTool shareSSCollectionDataToolWithModelType:CityModel] deleteCollectoinData:self.cityDataArr[indexPath.row] modelType:CityModel];
            [self.cityDataArr removeObjectAtIndex:indexPath.row];
            [self.cityTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
            
            if (self.cityDataArr.count == 0) {
                self.citPlace.hidden = NO;
            }
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)selectSaveList
{
    self.bigScrollor.contentOffset = CGPointMake(self.saveSegVC.selectedSegmentIndex * WIDTH, 0);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.saveSegVC.selectedSegmentIndex = scrollView.contentOffset.x / WIDTH;
}
// 创建segVC
- (void)createSegVC
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" <返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
   
    self.saveSegVC = [[UISegmentedControl alloc] initWithItems:@[@"折扣",@"城市",@"国家"]];
    self.saveSegVC.frame = CGRectMake((WIDTH-150)/2, 7, 150, 30);
    self.navigationItem.titleView = self.saveSegVC;
    self.saveSegVC.layer.cornerRadius = 15;
    self.saveSegVC.layer.borderWidth = 1;
    self.saveSegVC.layer.borderColor = [UIColor colorWithRed:0.050 green:0.404 blue:0.820 alpha:1.000].CGColor;
    self.saveSegVC.layer.masksToBounds = YES;
    self.saveSegVC.selectedSegmentIndex = 0;
    [self.saveSegVC addTarget:self action:@selector(selectSaveList) forControlEvents:UIControlEventValueChanged];

    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [self.saveSegVC setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: [UIColor colorWithWhite:0.145 alpha:1.000]};
    [self.saveSegVC setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
}
@end
